package com.cxd.app.controller;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.cxd.app.service.LoanService;
import com.cxd.app.service.UserService;
import com.cxd.base.common.exception.CxdException;
import com.cxd.base.common.exception.VersionException;
import com.cxd.base.controller.BaseController;
import com.cxd.base.util.BeanTools;

/**
 * <一句话功能简述> <功能详细描述>
 * @author wtj
 * @date 2015年12月4日 下午2:52:56
 * @company CXD
 * @version [Copyright (c) 2015 V001]
 */
@Controller("LoanController")
@RequestMapping("loanc")
public class LoanController extends BaseController{
	private static final Logger logger = Logger.getLogger(UserController.class);
	
	@Resource
	public BeanTools bt;
	
	@Resource
	public LoanService loanService;
	
	@Resource
	public UserService userService;
	/**
	 * 获取新手标
	 * @param request
	 * @param response
	 * @param value
	 */
	@RequestMapping(value = "loadNewerLoan", method = {RequestMethod.GET,RequestMethod.POST })
	public void loadNewerLoan(HttpServletRequest request,HttpServletResponse response,String value)throws IOException{
		JSONObject json = new JSONObject();
		try {
			loanService.loadNewerLoan(json, value);
			if(json.get("status")==null){
				this.jsonMsg(json, 200, "成功");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(response, json);
	}
	
	/**
	 * 根据表ID获取标信息
	 * @author wtj
	 * @date 2015年12月5日下午2:23:22
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "loan", method = {RequestMethod.GET,RequestMethod.POST })
	public void loan(HttpServletRequest request,HttpServletResponse response,String value)throws IOException{
		super.requestLog("loan",value);
		JSONObject json = new JSONObject();
		try {
			loanService.loan(json, value);
			if(json.get("status")==null){
				this.jsonMsg(json, 200, "成功");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(response, JSON.toJSONStringWithDateFormat(json, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat));
	}
	/**
	 * 获取项目列表
	 * @author wtj
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "loans", method = {RequestMethod.GET,RequestMethod.POST })
	public void loans(HttpServletRequest request,HttpServletResponse response,String value)throws IOException{
		super.requestLog("loans",value);
		Long beginTime = System.currentTimeMillis();
		JSONObject json = new JSONObject();
		try {
			loanService.loans(json, value);
			if(json.get("status")==null){
				this.jsonMsg(json, 200, "成功");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(beginTime,response, JSON.toJSONStringWithDateFormat(json, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat));
	}
	
	/**
	 * 项目投资
	 * @author wtj
	 * @date 2015年12月5日下午2:35:44
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "invest", method = {RequestMethod.GET,RequestMethod.POST })
	public void inves(HttpServletRequest request,HttpServletResponse response,String value)throws IOException{
		super.requestLog("invest",value);
		JSONObject json = new JSONObject();
		try {
			if(!userService.checkToken(request)){
				json.put("status", 403);
				json.put("msg", "用户未登录");
			}else{
				try {
					loanService.invest(json, value);
				} catch (VersionException e) {
					loanService.invest(json, value);
				}
				if(json.get("status")==null){
					this.jsonMsg(json, 200, "成功");
				}
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
		}catch(VersionException e){
			json.put("status", 500);
			json.put("msg", "投资失败,请刷新后重新投资");
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		logger.info("请求信息:"+value+"----投资信息"+json.toString());
		super.responseSendMsg(response, JSON.toJSONStringWithDateFormat(json, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat));
	}
	
	/**
	 * 获取用户投资记录 我的投资
	 * @author wtj
	 * @date 2015年12月7日上午9:25:20
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "userInvests", method = {RequestMethod.GET,RequestMethod.POST })
	public void userInvests(HttpServletRequest request,HttpServletResponse response,String value)throws IOException{
		super.requestLog("userInvests",value);
		Long beginTime = System.currentTimeMillis();
		JSONObject json = new JSONObject();
		try {
			loanService.userInvests(json, value);
			if(json.get("status")==null){
				this.jsonMsg(json, 200, "成功");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(beginTime,response, JSON.toJSONStringWithDateFormat(json, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat));
	}
	
	/**
	 * 项目还款计划
	 * @author wtj
	 * @date 2015年12月8日下午3:24:18
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "repayPlan", method = {RequestMethod.GET,RequestMethod.POST })
	public void repayPlan(HttpServletRequest request,HttpServletResponse response,String value)throws IOException{
		JSONObject json = new JSONObject();
		try {
			loanService.repayPlan(json, value);
			if(json.get("status")==null){
				this.jsonMsg(json, 200, "成功");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(response, JSON.toJSONStringWithDateFormat(json, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat));
	}
	
	/**
	 * 项目投资记录
	 * @author wtj
	 * @date 2015年12月8日下午3:50:24
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "investRecords", method = {RequestMethod.GET,RequestMethod.POST })
	public void investRecords(HttpServletRequest request,HttpServletResponse response,String value)throws IOException{
		JSONObject json = new JSONObject();
		try {
			loanService.investRecords(json, value);
			if(json.get("status")==null){
				this.jsonMsg(json, 200, "成功");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(response, JSON.toJSONStringWithDateFormat(json, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat));
	}
}
